<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp" %>
<html>
 <head>
    <title>仓库信息管理</title>
    <!-- import css and js -->
	<%@ include file="/common/meta.jsp" %>
	<%@ include file="/common/metaInput.jsp" %>  
	<script src="${ctx}/js/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
	<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
	<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
	<script type="text/javascript">
		var basic_Url="${ctx}/yd/logic/poware";
		var ItemStateList = ItemStateData.Rows;
		var item = eval("("+'${poWareItemBatch}'+")");
		var wareitemid = item.wareitemid;
		var code = item.code;
		var bd = '${itemBatchDOs}';
		//var delBd = '${delItemBatchDOs}';
 		//alert("bd:"+bd);
// 		function createSearch(){
// 			init(1,6,'100,100,100,100,100,100','10%');
// 			//第一行的数据
// 			addCommon(0,0,"lable","物料编码：");
// 			addCommon(0,1,"txtlike","code");
// 			addCommon(0,2,"lable","物料名称：");
// 			addCommon(0,3,"txtlike","name");
// 			addCommon(0,4,"lable","规格型号：");
// 			addCommon(0,5,"txtlike","spec");
// 		}
		
// 		function searchAP(){
// 			multipleSearch("#table1",true,"",basic_Url+"/itemlist.do?componentSequenceId="+componentSequenceId);
// 		}
		var grid;
		$(function(){
			var column1 =  [
							  { display: 'id', name: 'id', hide:1 },
							  { display: 'wareitemid', name: 'wareitemid', hide:1 },
							  { display: '物料id', name: 'materialsid', hide:1 },
							  { display: '<font color="gray">物料编码</font>', name: 'code', width: 180},
			                  { display: '<font color="gray">物料名称</font>', name: 'name', width: 180 },
			                  { display: '<font color="gray">型号规格</font>', name: 'spec', width: 245 },
			                  { display: '<font color="gray">基本单位</font>', name: 'junit', width: 120},
			                  { display: '<font color="gray">基本单位订单数量</font>', name: 'healthycount', width: 120},
			                  { display: '<font color="gray">单位</font>', name: 'unit', width: 120},
			                  { display: '<font color="gray">订单数量</font>', name: 'badcount', width: 120},
			                  { display: '批次<font color="red">*</font>', name: 'batch' ,width: 150,editor: { type: 'text'}},
			                  { display: '状态<font color="red">*</font>', name: 'state', width: 120,editor: { type: 'select', data: ItemStateList, valueColumnName: 'ItemStateID', displayColumnName: 'ItemStateName' }, render: function (item)
				                   {
				                   for (var i = 0; i < ItemStateList.length; i++)
				                   {
				                       if (ItemStateList[i]['ItemStateID'] == item.state){
				                     	  return ItemStateList[i]['ItemStateName'];
				                       }
				                   }
				                   return item.state;
				               }},
			                  { display: '<font color="gray">基本单位已入库数量</font>', name: 'inCount', width: 120},
			                  { display: '<font color="gray">基本单位已退货数量</font>', name: 'jreturnCount', width: 120},
			                  { display: '<font color="gray">已退货数量</font>', name: 'returnCount', width: 120},
			                  { display: '<font color="gray">基本单位入库数量</font>', name: 'jquantity', width: 120},
			                  { display: '入库数量<font color="red">*</font>', name: 'quantity', width: 120,editor: { type: 'float'},render:function(item){
			                	  if((item.quantity!=null&& !isNaN(item.quantity)) && (item.unitconvert != null && !isNaN(item.unitconvert))){
			                		  grid.updateCell('jquantity', (item.unitconvert*item.quantity).toFixed(4), item);
			            	   		}
		                           	return (item.quantity==undefined ? 0:(parseFloat(item.quantity)).toFixed(4));
				               }},
				               { display: '<font color="gray">单位换算率</font>', name: 'unitconvert', width: 120}
			                  ];
			var column2 =  [
							   { display: 'id', name: 'id', hide:1 },
							   { display: 'wareitemid', name: 'wareitemid', hide:1 },
							  { display: '物料id', name: 'materialsid', hide:1 },
							  { display: '<font color="gray">物料编码</font>', name: 'code', width: 180},
			                  { display: '<font color="gray">物料名称</font>', name: 'name', width: 180 },
			                  { display: '<font color="gray">型号规格</font>', name: 'spec', width: 245 },
			                  { display: '<font color="gray">基本单位</font>', name: 'junit', width: 120},
			                  { display: '<font color="gray">基本单位订单数量</font>', name: 'healthycount', width: 120},
			                  { display: '<font color="gray">单位</font>', name: 'unit', width: 120},
			                  { display: '<font color="gray">订单数量</font>', name: 'badcount', width: 120},
			                  { display: '批次<font color="red">*</font>', name: 'batch' ,width: 150},
			                  { display: '状态<font color="red">*</font>', name: 'state', width: 120,
			                	  render: function (item){
			                		  var state=item.state;
			                		  if(state=="1"){
			                			  return "一等品";
			                		  }
			                		  if(state=="2"){
			                			  return "次品";
			                		  }
			                		  if(state=="3"){
			                			  return "旧件";
			                		  }
			                	  }},
			                  { display: '<font color="gray">基本单位已入库数量</font>', name: 'inCount', width: 120},
			                  { display: '<font color="gray">基本单位已退货数量</font>', name: 'jreturnCount', width: 120},
			                  { display: '<font color="gray">已退货数量</font>', name: 'returnCount', width: 120},
			                  { display: '<font color="gray">基本单位入库数量</font>', name: 'jquantity', width: 120},
			                  { display: '入库数量<font color="red">*</font>', name: 'quantity', width: 120},
				               { display: '<font color="gray">单位换算率</font>', name: 'unitconvert', width: 120}
			                  ];
			//var colunm3 = column1;                 
// 		    if('${billstatus}' != "" && '${billstatus}' != "0"){
// 		    	colunm3 = column2;    
// 		    }
 if('${billstatus}' != "" && '${billstatus}' != "0"){
	 grid = $("#table1").ligerGrid({
	        columns: column2, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?wareitemid="+wareitemid+"&code="+code+"&itembatch="+encodeURIComponent(bd),
	                      width: "100%", height:"98%"
	                  });
 }else{
	 grid = $("#table1").ligerGrid({
	        columns: column1, 
	                      enabledEdit: true,
	                      usePager: true,pageSize:1000,pageSizeOptions:[1000],enabledSort:false, checkbox: true,
	                      url : basic_Url+"/itemlist.do?wareitemid="+wareitemid+"&code="+code+"&itembatch="+encodeURIComponent(bd),
	                      width: "100%", height:"98%",
	                      toolbar: { items: [
	                                         { text: '添加', click: insert, icon: 'add' },
	                                         { line: true },
	                                         { text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }
	                                         ]
	                                         }
	                  });
 }
				
				wareitemid ='';
				code ='';
			$("#pageloading").hide();
		
// 		<c:forEach items='${wareItemBatchDOs}' var='ib'>
// 		var gm = $("#table1").ligerGetGridManager();
// 		alert("进来了吗");
// 		gm.addRow2({
// 	    		id: "${ib.id}",
// 	    		wareitemid:"${ib.wareitemid}",
// 	    		materialsid: "${ib.materialsid}",
// 	    		code: "${ib.code}",
// 	    		name: "${ib.name}",
// 	    		spec: "${ib.spec}",
// 	    		junit : "${ib.junit}",
// 	    		healthycount : "${ib.healthycount}",
// 	    		unit : "${ib.unit}",
// 	   	   		batch : "${ib.batch}",
// 	    		badcount : "${ib.badcount}",
// 	    		inCount : "${ib.inCount}",
// 	    		jquantity : "${ib.jquantity}",
// 	    		quantity :"${ib.quantity}",
// 	    		state : "${ib.state}",
// 	    		unitconvert : "${ib.unitconvert}"
// 	        });
// 	    </c:forEach>
		
		});
		
		function getSelected(){
			var rows = grid.getCheckedRows();
			 return rows;
		}
		
		function Close(){
			CloseDialog();
		}
		function insert(){
			if('${billstatus}' != "" && '${billstatus}' != "0"){
				return false;
			}else{
				var manager = $("#table1").ligerGetGridManager();
				manager.addRow2({
					wareitemid:item.wareitemid,
		    		materialsid: item.materialsid,
		    		code: item.code,
		    		name: item.name,
		    		spec: item.spec,
		    		junit : item.junit,
		    		healthycount : item.healthycount,
		    		unit : item.unit,
		   	   		//batch : item.batch,
		    		badcount : item.badcount,
		    		inCount : item.inCount,
		    		//jquantity : item.jquantity,
		    		//quantity :item.quantity,
		    		//state : item.state,
		    		unitconvert : item.unitconvert
				});
			}
		}
		function deleteRow()
		{ if('${billstatus}' != "" && '${billstatus}' != "0"){
			return false;
		}else{
			grid.deleteSelectedRow();
		}
		}
		var ba=true;
		var num = 0;
		var jnum = 0;
		function yz(){
			var data = grid.getData();
			if (data != null && data.length > 0) {
				jnum = 0;
				num = 0;
				for ( var i = 0; i < data.length; i++) {
					if(data[i].batch==undefined ||data[i].batch.replace(/(^\s*)|(\s*$)/g, "")=="" ||data[i].batch==null){
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的批次没有填写");
	 					return false;
	 				}
	 				if(data[i].state=="" ||data[i].state==undefined ||data[i].state==null){
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的状态没有填写");
	 					return false;
	 				}
	 				for ( var j = 0; j < data.length; j++) {
	 					if(i != j){
	 						if(data[i].code==data[j].code && data[i].batch==data[j].batch && data[i].state==data[j].state){
	 							ba = false;
	 							$.dialog.alert("第"+(i+1)+"行与第"+(j+1)+"行存在相同的批次与等级!");
	 							return false;
	 						}
	 					}
	 				}
	 				jnum += parseFloat(data[i].jquantity);
	 				num  += parseFloat(data[i].quantity);
	 				if (isNaN(data[i].quantity) ||data[i].quantity==undefined ||data[i].quantity==null) {
	 					ba = false;
	 					$.dialog.alert("您第" + (i+1) + "行的数量没有填写");
	 					return false;
	 				}
	 				if (data[i].quantity <= 0) {
	 					$.dialog.alert("您第" + (i+1) + "行的入库数量不能小于等于0");
	 					ba = false;
	 					return false;
	 				}else if(jnum>(data[i].healthycount - (data[i].inCount == null ? 0.0 : data[i].inCount))){
							ba = false;
							$.dialog.alert("入库数量合计不能大于订单未入库数量!");
							return false;
						}
	 			}
			}
				ba=true;
		}
	</script>
	
  </head>
 <body style="padding:10px">  
       <div id="layout1">
       <div id="layout1">
<!--           <div class="panelBar"> -->
<!-- 	            <ul class="toolBar"> -->
<!-- 		           <li><a class="add" onclick="insert()"><span>新增</span></a></li> -->
<!-- 		       		<li><a class="delete" onclick="deleteRow()"><span>删除</span></a></li> -->
<!--                </ul> -->
<!--           </div> -->
          <div id="pageHeader" class="pageHeader">
               <div class="searchBar" id="multipleSearchDialog">
	                <table class="table" width="100%" id="searchHeader" >  
                        <tbody id="newbody">
                        </tbody>
 	                </table>
<!--                     <div class="subBar"> -->
<!-- 			             <ul> -->
<!-- 				             <li><div class="buttonActive"><div class="buttonContent"><button type="submit" id="btnSearch" onclick="insert()">添加</button></div></div></li> -->
<!--  				             <li><a class="button" onclick="clearSearch()"><span>删除</span></a></li>  -->
<!-- 			             </ul> -->
<!-- 	                </div> -->
               </div>
          </div>
      </div>
      <div position="center" id="table1">
      </div>
      <div class="clear"></div>
   </div>
    </body>
</html>